﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Xml.Serialization;

namespace OrderManageSystem
{
    public class OrderDetails
    {
        public string name;
        public string Name
        {
            get { return name; }
            set
            {
                if (value != null)
                    name = value;
                else
                    Console.WriteLine("商品名称不能为空。");
            }
        }
        public int price;
        public int Price
        {
            get { return price; }
            set
            {
                if (value > 0)
                    price = value;
                else
                    Console.WriteLine("商品价格不合法。");
            }
        }
        public int amount;//购买数量
        public int Amount
        {
            get { return amount; }
            set
            {
                if (value >= 0)
                    amount = value;
                else
                    Console.WriteLine("商品的购买数量不合法。");
            }
        }
        public OrderDetails()
        {
            price = 0;
            amount = 0;
            name = "";
        }
        public OrderDetails(string n, int p, int a)
        {
            price = p;
            amount = a;
            name = n;
        }
        public int GetTotalPrice()
        {
            return price * amount;
        }
    }


    public class Order
    {
        public int ID { get; set; }//订单编号
        public string CustomerName { get; set; }//客户姓名
        public int TotalPrice { get; set; }//总价

        public List<OrderDetails> OrderList = new List<OrderDetails>();//列表

        public Order()
        {
            ID = 0;
            CustomerName = "";
            TotalPrice = 0;
        }
        public Order(int id, string name)
        {
            ID = id;
            CustomerName = name;
        }
        public override bool Equals(object obj)
        {
            if (obj is Order)
            {
                Order o = (Order)obj;
                if (o.ID == ID)
                    return true;
            }
            return false;
        }
        public override int GetHashCode()
        {
            return this.ID;
        }
        public int getTotalPriceofList()
        {
            int price = 0;
            foreach (OrderDetails od in OrderList)
            {
                price += od.GetTotalPrice();
            }
            TotalPrice = price;
            return price;
        }
        public void addItem(string n, int p, int a)
        {
            OrderList.Add(new OrderDetails(n, p, a));
        }
        public void RemoveOrderList(int index)
        {
            try
            {
                OrderList.RemoveAt(index);
                Console.WriteLine("删除订单成功！");
            }
            catch
            {
                Console.WriteLine("删除订单失败！");
            }
        }
        public void show()
        {
            Console.WriteLine("商品" + '\t' + "数量" + '\t' + "单价");
            foreach (OrderDetails od in OrderList)
            {
                Console.WriteLine(od.Name + '\t' + od.Amount + '\t' + od.Price);
            }
        }
    }

    public class OrderService
    {
        public List<Order> OrderServiceList = new List<Order>();
        public Order addItemtoOrderServiceList(int id, string cname)
        {
            Order addorder = new Order(id, cname);
            OrderServiceList.Add(addorder);
            Console.WriteLine("请输入订单明细：");
            bool flag = true;
            while (flag)
            {
                Console.WriteLine("请输入商品名称：");
                string name = Console.ReadLine();
                Console.WriteLine("请输入购买数量：");
                int account = int.Parse(Console.ReadLine());
                Console.WriteLine("请输入单价：");
                int price = int.Parse(Console.ReadLine());
                addorder.addItem(name, price, account);
                Console.WriteLine("是否继续添加订单明细(输入yes/no)：");
                string s = Console.ReadLine();
                if (s == "no")
                    flag = false;
                else
                    continue;
            }
            Console.WriteLine("添加完成");
            return addorder;
        }
        public void showorder(List<Order> o)
        {
            Console.WriteLine("ID" + '\t' + "客户名" + '\t' + "总价格");
            foreach (Order obj in o)
            {
                Console.WriteLine(obj.ID + "       " + obj.CustomerName + '\t' + obj.getTotalPriceofList());
                obj.show();
            }
        }
        public List<Order> removeOrder(List<Order> order, int id)
        {
            int index = 0;
            foreach (Order obj in order)
            {
                if (obj.ID == id)
                    index = order.IndexOf(obj);
                Console.WriteLine("输入1删除整个订单，输入2删除该订单中的订单明细");
                int a = int.Parse(Console.ReadLine());
                if (a == 1)
                {
                    order.RemoveAt(index);
                    Console.WriteLine("删除完成");
                }
                else if (a == 2)
                {
                    order[index].show();
                    Console.WriteLine("请输入要删除的商品序号：");
                    int index2 = int.Parse(Console.ReadLine());
                    order[index].OrderList.RemoveAt(index2);
                }
            }
            return order;
        }
        public void searchbyname(List<Order> list, string cname)
        {
            var var1 = from obj in list
                       where obj.CustomerName == cname
                       orderby obj.getTotalPriceofList()
                       select obj;
            List<Order> SearchOrderList = var1.ToList();
            Console.WriteLine("ID" + '\t' + "客户姓名" + '\t' + "总价格");
            foreach (Order obj in SearchOrderList)
            {
                Console.WriteLine(obj.ID + "       " + obj.CustomerName + '\t' + obj.getTotalPriceofList());
                obj.show();
            }
        }
        public void searchbyid(List<Order> list, int id)
        {
            var var2 = from obj in list
                       where obj.ID == id
                       orderby obj.getTotalPriceofList()
                       select obj;
            List<Order> SearchOrderList = var2.ToList();
            Console.WriteLine("ID" + '\t' + "客户姓名" + '\t' + "总价格");
            foreach (Order obj in SearchOrderList)
            {
                Console.WriteLine(obj.ID + "       " + obj.CustomerName + '\t' + obj.getTotalPriceofList());
                obj.show();
            }
        }
        public void ModifyOrderListbyName(List<Order> list, string cname)
        {
            foreach (Order obj in list)
            {
                if (obj.CustomerName == cname)
                {
                    Console.WriteLine("请输入1修改订单信息，输入2修改订单明细。");
                    int a = int.Parse(Console.ReadLine());
                    switch (a)
                    {
                        case 1:
                            {
                                Console.WriteLine("请输入需要修改的信息：1表示ID，2表示客户名字：");
                                int b = int.Parse(Console.ReadLine());
                                if (b == 1)
                                {
                                    Console.WriteLine("请输入修改后的ID：");
                                    int c = int.Parse(Console.ReadLine());
                                    obj.ID = c;
                                }
                                else
                                {
                                    Console.WriteLine("请输入修改后的名字：");
                                    string d = Console.ReadLine();
                                    obj.CustomerName = d;
                                }
                                break;
                            }
                        case 2:
                            {
                                obj.show();
                                Console.WriteLine("请输入需要修改的序号：");
                                int f = int.Parse(Console.ReadLine());
                                Console.WriteLine("请输入需要修改的信息：1表示名称，2表示价格，3表示购买数量：");
                                int g = int.Parse(Console.ReadLine());
                                switch (g)
                                {
                                    case 1:
                                        {
                                            Console.WriteLine("请输入修改后的名字：");
                                            string h = Console.ReadLine();
                                            obj.OrderList[f].Name = h;
                                            break;
                                        }
                                    case 2:
                                        {
                                            Console.WriteLine("请输入修改后的价格：");
                                            int i = int.Parse(Console.ReadLine());
                                            obj.OrderList[f].Price = i;
                                            break;
                                        }
                                    case 3:
                                        {
                                            Console.WriteLine("请输入修改后的买入数量：");
                                            int j = int.Parse(Console.ReadLine());
                                            obj.OrderList[f].Amount = j;
                                            break;
                                        }
                                    default:
                                        break;
                                }
                                break;
                            }
                        default:
                            break;
                    }
                }
            }
        }
        public bool Export()
        {
            try
            {
                XmlSerializer XML = new XmlSerializer(typeof(List<Order>));
                using (FileStream fs=new FileStream("Orders.xml",FileMode.Create))
                {
                    XML.Serialize(fs, OrderServiceList);
                }
                return true;
            }
            catch
            {
                return false;
            }
        }
        public bool Import()
        {
            try
            {
                XmlSerializer XMLread = new XmlSerializer(typeof(List<Order>));
                using (FileStream fs = new FileStream("Orders.xml",FileMode.Open))
                {
                    OrderServiceList=(List<Order>)XMLread.Deserialize(fs);
                }
                return true;
            }
            catch
            {
                return false;
            }
        }
    }
    public class Test
    {
        public static void Main(string[] args)
        {
            OrderService O = new OrderService();
            bool exit = true;
            while (exit)
            {
                Console.WriteLine("请输入操作序号：");
                Console.WriteLine("1：增加订单，2：删除订单，3：查询订单，4：显示所有订单，5：修改订单,6：退出");
                int choose = int.Parse(Console.ReadLine());
                switch (choose)
                {
                    case 1:
                        Console.WriteLine("请输入客户名字：");
                        string name = Console.ReadLine();
                        OrderService od = new OrderService();
                        Console.WriteLine("请输入客户ID：");
                        int id = int.Parse(Console.ReadLine());
                        O.addItemtoOrderServiceList(id, name);
                        break;
                    case 2:
                        Console.WriteLine("输入要删除的订单ID：");
                        int oid = int.Parse(Console.ReadLine());
                        O.removeOrder(O.OrderServiceList, oid);
                        break;
                    case 3:
                        Console.WriteLine("1:根据ID查询，2：根据客户名字查询");
                        int i = int.Parse(Console.ReadLine());
                        switch (i)
                        {
                            case 1:
                                Console.WriteLine("请输入ID：");
                                int idid = int.Parse(Console.ReadLine());
                                O.searchbyid(O.OrderServiceList, idid);
                                break;
                            case 2:
                                Console.WriteLine("请输入客户名：");
                                string cname = Console.ReadLine();
                                O.searchbyname(O.OrderServiceList, cname);
                                break;
                            default:
                                break;
                        }
                        break;
                    case 4:
                        O.showorder(O.OrderServiceList);
                        break;
                    case 5:
                        Console.WriteLine("请输入需要修改订单的客户姓名：");
                        string Cname = Console.ReadLine();
                        O.ModifyOrderListbyName(O.OrderServiceList, Cname);
                        break;
                    case 6:
                        exit = false;
                        System.Environment.Exit(0);
                        break;
                }
            }
        }
    }
}